#include "breathrate.h"
#include "breathrate_private.h"

B_breathrate_T breathrate_B;
DW_breathrate_T breathrate_DW;
ExtU_breathrate_T breathrate_U;
ExtY_breathrate_T breathrate_Y;
RT_MODEL_breathrate_T breathrate_M_;
RT_MODEL_breathrate_T *const breathrate_M = &breathrate_M_;
static void breathrate_getAllPeaksCodegen(const real32_T y[149], int32_T
    iPk_data[], int32_T *iPk_size, int32_T *iInf_size, int32_T iInflect_data[],
    int32_T *iInflect_size);
static void breathrate_removeSmallPeaks(const real32_T y[149], const int32_T
    iFinite_data[], const int32_T *iFinite_size, int32_T iPk_data[], int32_T
    *iPk_size);
static void breathrate_do_vectors(const int32_T a_data[], const int32_T *a_size,
    int32_T c_data[], int32_T *c_size, int32_T ia_data[], int32_T *ia_size,
    int32_T *ib_size);
static void breathrate_sortIdx(const real32_T x_data[], const int32_T *x_size,
    int32_T idx_data[], int32_T *idx_size);
static void breathrate_merge(int32_T idx_data[], int32_T x_data[], int32_T
    offset, int32_T np, int32_T nq, int32_T iwork_data[], int32_T xwork_data[]);
static void breathrate_merge_block(int32_T idx_data[], int32_T x_data[], int32_T
    n, int32_T iwork_data[], int32_T xwork_data[]);
static void breathrate_sortIdx_c(int32_T x_data[], int32_T *x_size, int32_T
    idx_data[], int32_T *idx_size);
static void breathrate_sort(int32_T x_data[], int32_T *x_size);
static void findPeaksSeparatedByMoreThanMin(const real32_T y[149], const int32_T
    iPk_data[], const int32_T *iPk_size, int32_T idx_data[], int32_T *idx_size);
static void breathrate_findpeaks(const real32_T Yin[149], real32_T Ypk_data[],
    int32_T *Ypk_size, real_T Xpk_data[], int32_T *Xpk_size);
static void breathrate_diff(const real_T x_data[], const int32_T *x_size, real_T
    y_data[], int32_T *y_size);
static void breathrate_SystemCore_release(dspcodegen_BiquadFilter_breat_T *obj);
static void brea_LPHPFilterBase_releaseImpl(dsp_LowpassFilter_breathrate_T *obj);
static void breat_SystemCore_releaseWrapper(dsp_LowpassFilter_breathrate_T *obj);
static void breathrate_SystemCore_release_l(dsp_LowpassFilter_breathrate_T *obj);
static void breathrate_SystemCore_delete_l(dsp_LowpassFilter_breathrate_T *obj);
static void matlabCodegenHandle_matlabCod_l(dsp_LowpassFilter_breathrate_T *obj);
static void breathrate_SystemCore_delete(dspcodegen_BiquadFilter_breat_T *obj);
static void matlabCodegenHandle_matlabCodeg(dspcodegen_BiquadFilter_breat_T *obj);
static void breathrate_getAllPeaksCodegen(const real32_T y[149], int32_T
    iPk_data[], int32_T *iPk_size, int32_T *iInf_size, int32_T iInflect_data[],
    int32_T *iInflect_size)
{
    int32_T nPk;
    int32_T nInflect;
    char_T dir;
    int32_T kfirst;
    real32_T ykfirst;
    char_T previousdir;
    int32_T k;
    real32_T tmp;
    nPk = 0;
    nInflect = -1;
    dir = 'n';
    kfirst = 1;
    ykfirst = y[0];
    for (k = 0; k < 148; k++)
    {
        tmp = y[k + 1];
        if (tmp != ykfirst)
        {
            previousdir = dir;
            if (tmp < ykfirst)
            {
                dir = 'd';
                if ('d' != (int8_T)previousdir)
                {
                    nInflect++;
                    iInflect_data[nInflect] = kfirst;
                    if ((int8_T)previousdir == 'i')
                    {
                        nPk++;
                        iPk_data[nPk - 1] = kfirst;
                    }
                }
            }
            else
            {
                dir = 'i';
                if ('i' != (int8_T)previousdir)
                {
                    nInflect++;
                    iInflect_data[nInflect] = kfirst;
                }
            }

            ykfirst = tmp;
            kfirst = k + 2;
        }
    }

    if ((nInflect + 1 == 0) || (iInflect_data[nInflect] < 149))
    {
        nInflect++;
        iInflect_data[nInflect] = 149;
    }

    if (1 > nPk)
    {
        nPk = 0;
    }

    for (kfirst = 0; kfirst < nPk; kfirst++)
    {
        breathrate_B.iPk_data_c[kfirst] = (uint8_T)iPk_data[kfirst];
    }

    *iPk_size = nPk;
    for (kfirst = 0; kfirst < nPk; kfirst++)
    {
        iPk_data[kfirst] = (int32_T)breathrate_B.iPk_data_c[kfirst];
    }

    *iInf_size = 0;
    if (1 > nInflect + 1)
    {
        nPk = 0;
    }
    else
    {
        nPk = nInflect + 1;
    }

    for (kfirst = 0; kfirst < nPk; kfirst++)
    {
        breathrate_B.iPk_data_c[kfirst] = (uint8_T)iInflect_data[kfirst];
    }

    *iInflect_size = nPk;
    for (kfirst = 0; kfirst < nPk; kfirst++)
    {
        iInflect_data[kfirst] = (int32_T)breathrate_B.iPk_data_c[kfirst];
    }
}

static void breathrate_removeSmallPeaks(const real32_T y[149], const int32_T
    iFinite_data[], const int32_T *iFinite_size, int32_T iPk_data[], int32_T
    *iPk_size)
{
    int32_T nPk;
    int32_T k;
    real32_T u0;
    real32_T tmp;
    nPk = 0;
    for (k = 0; k < *iFinite_size; k++)
    {
        tmp = y[iFinite_data[k] - 1];
        if (tmp > 5.0F)
        {
            u0 = y[iFinite_data[k] - 2];
            if (u0 <= y[iFinite_data[k]])
            {
                u0 = y[iFinite_data[k]];
            }

            if (tmp - u0 >= 0.0F)
            {
                nPk++;
                iPk_data[nPk - 1] = iFinite_data[k];
            }
        }
    }

    if (1 > nPk)
    {
        *iPk_size = 0;
    }
    else
    {
        *iPk_size = nPk;
    }
}

static void breathrate_do_vectors(const int32_T a_data[], const int32_T *a_size,
    int32_T c_data[], int32_T *c_size, int32_T ia_data[], int32_T *ia_size,
    int32_T *ib_size)
{
    int32_T nc;
    int32_T nia;
    int32_T ialast;
    int32_T b_ialast;
    *c_size = (int32_T)(uint8_T)*a_size;
    *ia_size = *a_size;
    *ib_size = 0;
    nc = 0;
    nia = 0;
    for (ialast = 1; ialast <= *a_size; ialast = b_ialast + 1)
    {
        b_ialast = ialast;
        while ((b_ialast < *a_size) && (a_data[ialast - 1] == a_data[b_ialast]))
        {
            b_ialast++;
        }

        nc++;
        nia++;
        c_data[nc - 1] = a_data[ialast - 1];
        ia_data[nia - 1] = ialast;
    }

    if (*a_size > 0)
    {
        if (1 > nia)
        {
            *ia_size = 0;
        }
        else
        {
            *ia_size = nia;
        }

        if (1 > nc)
        {
            *c_size = 0;
        }
        else
        {
            *c_size = nc;
        }
    }
}

static void breathrate_sortIdx(const real32_T x_data[], const int32_T *x_size,
    int32_T idx_data[], int32_T *idx_size)
{
    int32_T n;
    int32_T k;
    int32_T i;
    int32_T i2;
    int32_T j;
    int32_T pEnd;
    int32_T p;
    int32_T q;
    int32_T qEnd;
    int32_T kEnd;
    uint8_T b_idx_0;
    int32_T tmp;
    int32_T tmp_0;
    n = *x_size + 1;
    b_idx_0 = (uint8_T)*x_size;
    *idx_size = (int32_T)b_idx_0;
    if (0 <= (int32_T)b_idx_0 - 1)
    {
        memset(&idx_data[0], 0, (uint32_T)b_idx_0 * sizeof(int32_T));
    }

    for (i = 1; i <= n - 2; i += 2)
    {
        if (x_data[i - 1] >= x_data[i])
        {
            idx_data[i - 1] = i;
            idx_data[i] = i + 1;
        }
        else
        {
            idx_data[i - 1] = i + 1;
            idx_data[i] = i;
        }
    }

    if (((uint32_T)*x_size & 1U) != 0U)
    {
        idx_data[*x_size - 1] = *x_size;
    }

    i = 2;
    while (i < n - 1)
    {
        i2 = i << 1;
        j = 1;
        pEnd = 1 + i;
        while (pEnd < n)
        {
            p = j;
            q = pEnd;
            qEnd = j + i2;
            if (qEnd > n)
            {
                qEnd = n;
            }

            k = 0;
            kEnd = qEnd - j;
            while (k + 1 <= kEnd)
            {
                tmp = idx_data[q - 1];
                tmp_0 = idx_data[p - 1];
                if (x_data[tmp_0 - 1] >= x_data[tmp - 1])
                {
                    breathrate_B.iwork_data_k[k] = tmp_0;
                    p++;
                    if (p == pEnd)
                    {
                        while (q < qEnd)
                        {
                            k++;
                            breathrate_B.iwork_data_k[k] = idx_data[q - 1];
                            q++;
                        }
                    }
                }
                else
                {
                    breathrate_B.iwork_data_k[k] = tmp;
                    q++;
                    if (q == qEnd)
                    {
                        while (p < pEnd)
                        {
                            k++;
                            breathrate_B.iwork_data_k[k] = idx_data[p - 1];
                            p++;
                        }
                    }
                }

                k++;
            }

            for (pEnd = 0; pEnd < kEnd; pEnd++)
            {
                idx_data[(j + pEnd) - 1] = breathrate_B.iwork_data_k[pEnd];
            }

            j = qEnd;
            pEnd = qEnd + i;
        }

        i = i2;
    }
}

static void breathrate_merge(int32_T idx_data[], int32_T x_data[], int32_T
    offset, int32_T np, int32_T nq, int32_T iwork_data[], int32_T xwork_data[])
{
    int32_T n;
    int32_T q;
    int32_T iout;
    int32_T n_tmp;
    int32_T exitg1;
    if (nq != 0)
    {
        n_tmp = np + nq;
        for (q = 0; q < n_tmp; q++)
        {
            iout = offset + q;
            iwork_data[q] = idx_data[iout];
            xwork_data[q] = x_data[iout];
        }

        n = 0;
        q = np;
        iout = offset - 1;
        do
        {
            exitg1 = 0;
            iout++;
            if (xwork_data[n] <= xwork_data[q])
            {
                idx_data[iout] = iwork_data[n];
                x_data[iout] = xwork_data[n];
                if (n + 1 < np)
                {
                    n++;
                }
                else
                {
                    exitg1 = 1;
                }
            }
            else
            {
                idx_data[iout] = iwork_data[q];
                x_data[iout] = xwork_data[q];
                if (q + 1 < n_tmp)
                {
                    q++;
                }
                else
                {
                    q = iout - n;
                    while (n + 1 <= np)
                    {
                        iout = (q + n) + 1;
                        idx_data[iout] = iwork_data[n];
                        x_data[iout] = xwork_data[n];
                        n++;
                    }

                    exitg1 = 1;
                }
            }
        }
        while (exitg1 == 0);
    }
}

static void breathrate_merge_block(int32_T idx_data[], int32_T x_data[], int32_T
    n, int32_T iwork_data[], int32_T xwork_data[])
{
    int32_T bLen;
    int32_T tailOffset;
    int32_T nTail;
    int32_T nPairs;
    nPairs = n >> 2;
    bLen = 4;
    while (nPairs > 1)
    {
        if (((uint32_T)nPairs & 1U) != 0U)
        {
            nPairs--;
            tailOffset = bLen * nPairs;
            nTail = n - tailOffset;
            if (nTail > bLen)
            {
                breathrate_merge(idx_data, x_data, tailOffset, bLen, nTail -
                                 bLen, iwork_data, xwork_data);
            }
        }

        tailOffset = bLen << 1;
        nPairs >>= 1;
        for (nTail = 0; nTail < nPairs; nTail++)
        {
            breathrate_merge(idx_data, x_data, nTail * tailOffset, bLen, bLen,
                             iwork_data, xwork_data);
        }

        bLen = tailOffset;
    }

    if (n > bLen)
    {
        breathrate_merge(idx_data, x_data, 0, bLen, n - bLen, iwork_data,
                         xwork_data);
    }
}

static void breathrate_sortIdx_c(int32_T x_data[], int32_T *x_size, int32_T
    idx_data[], int32_T *idx_size)
{
    int32_T i3;
    int32_T i4;
    int32_T c_x_size;
    uint8_T b_x_idx_0;
    uint8_T b_idx_0;
    int32_T tmp;
    int32_T tmp_0;
    b_x_idx_0 = (uint8_T)*x_size;
    b_idx_0 = (uint8_T)*x_size;
    *idx_size = (int32_T)b_x_idx_0;
    breathrate_B.nQuartets = (int32_T)b_x_idx_0;
    if (0 <= breathrate_B.nQuartets - 1)
    {
        memset(&idx_data[0], 0, (uint32_T)breathrate_B.nQuartets * sizeof
               (int32_T));
    }

    if (*x_size != 0)
    {
        c_x_size = *x_size;
        breathrate_B.nQuartets = *x_size;
        if (0 <= breathrate_B.nQuartets - 1)
        {
            memcpy(&breathrate_B.c_x_data[0], &x_data[0], (uint32_T)
                   breathrate_B.nQuartets * sizeof(int32_T));
        }

        *idx_size = (int32_T)b_idx_0;
        breathrate_B.nQuartets = (int32_T)b_idx_0;
        if (0 <= breathrate_B.nQuartets - 1)
        {
            memset(&idx_data[0], 0, (uint32_T)breathrate_B.nQuartets * sizeof
                   (int32_T));
        }

        breathrate_B.x4[0] = 0;
        breathrate_B.idx4[0] = 0;
        breathrate_B.x4[1] = 0;
        breathrate_B.idx4[1] = 0;
        breathrate_B.x4[2] = 0;
        breathrate_B.idx4[2] = 0;
        breathrate_B.x4[3] = 0;
        breathrate_B.idx4[3] = 0;
        breathrate_B.nQuartets = *x_size >> 2;
        breathrate_B.nLeft = 0;
        while (breathrate_B.nLeft <= breathrate_B.nQuartets - 1)
        {
            breathrate_B.i = breathrate_B.nLeft << 2;
            breathrate_B.idx4[0] = (int16_T)(breathrate_B.i + 1);
            breathrate_B.idx4[1] = (int16_T)(breathrate_B.i + 2);
            breathrate_B.idx4[2] = (int16_T)(breathrate_B.i + 3);
            breathrate_B.idx4[3] = (int16_T)(breathrate_B.i + 4);
            breathrate_B.x4[0] = breathrate_B.c_x_data[breathrate_B.i];
            breathrate_B.i1 = breathrate_B.c_x_data[breathrate_B.i + 1];
            breathrate_B.x4[1] = breathrate_B.i1;
            i3 = breathrate_B.c_x_data[breathrate_B.i + 2];
            breathrate_B.x4[2] = i3;
            i4 = breathrate_B.c_x_data[breathrate_B.i + 3];
            breathrate_B.x4[3] = i4;
            if (breathrate_B.c_x_data[breathrate_B.i] <= breathrate_B.i1)
            {
                breathrate_B.i1 = 1;
                breathrate_B.i2 = 2;
            }
            else
            {
                breathrate_B.i1 = 2;
                breathrate_B.i2 = 1;
            }

            if (i3 <= i4)
            {
                i3 = 3;
                i4 = 4;
            }
            else
            {
                i3 = 4;
                i4 = 3;
            }

            tmp = breathrate_B.x4[breathrate_B.i1 - 1];
            tmp_0 = breathrate_B.x4[i3 - 1];
            if (tmp <= tmp_0)
            {
                if (breathrate_B.x4[breathrate_B.i2 - 1] <= tmp_0)
                {
                    breathrate_B.perm[0] = (int8_T)breathrate_B.i1;
                    breathrate_B.perm[1] = (int8_T)breathrate_B.i2;
                    breathrate_B.perm[2] = (int8_T)i3;
                    breathrate_B.perm[3] = (int8_T)i4;
                }
                else if (breathrate_B.x4[breathrate_B.i2 - 1] <=
                         breathrate_B.x4[i4 - 1])
                {
                    breathrate_B.perm[0] = (int8_T)breathrate_B.i1;
                    breathrate_B.perm[1] = (int8_T)i3;
                    breathrate_B.perm[2] = (int8_T)breathrate_B.i2;
                    breathrate_B.perm[3] = (int8_T)i4;
                }
                else
                {
                    breathrate_B.perm[0] = (int8_T)breathrate_B.i1;
                    breathrate_B.perm[1] = (int8_T)i3;
                    breathrate_B.perm[2] = (int8_T)i4;
                    breathrate_B.perm[3] = (int8_T)breathrate_B.i2;
                }
            }
            else
            {
                tmp_0 = breathrate_B.x4[i4 - 1];
                if (tmp <= tmp_0)
                {
                    if (breathrate_B.x4[breathrate_B.i2 - 1] <= tmp_0)
                    {
                        breathrate_B.perm[0] = (int8_T)i3;
                        breathrate_B.perm[1] = (int8_T)breathrate_B.i1;
                        breathrate_B.perm[2] = (int8_T)breathrate_B.i2;
                        breathrate_B.perm[3] = (int8_T)i4;
                    }
                    else
                    {
                        breathrate_B.perm[0] = (int8_T)i3;
                        breathrate_B.perm[1] = (int8_T)breathrate_B.i1;
                        breathrate_B.perm[2] = (int8_T)i4;
                        breathrate_B.perm[3] = (int8_T)breathrate_B.i2;
                    }
                }
                else
                {
                    breathrate_B.perm[0] = (int8_T)i3;
                    breathrate_B.perm[1] = (int8_T)i4;
                    breathrate_B.perm[2] = (int8_T)breathrate_B.i1;
                    breathrate_B.perm[3] = (int8_T)breathrate_B.i2;
                }
            }

            breathrate_B.i1 = (int32_T)breathrate_B.perm[0] - 1;
            idx_data[breathrate_B.i] = (int32_T)
                breathrate_B.idx4[breathrate_B.i1];
            breathrate_B.i2 = (int32_T)breathrate_B.perm[1] - 1;
            idx_data[breathrate_B.i + 1] = (int32_T)
                breathrate_B.idx4[breathrate_B.i2];
            i3 = (int32_T)breathrate_B.perm[2] - 1;
            idx_data[breathrate_B.i + 2] = (int32_T)breathrate_B.idx4[i3];
            i4 = (int32_T)breathrate_B.perm[3] - 1;
            idx_data[breathrate_B.i + 3] = (int32_T)breathrate_B.idx4[i4];
            breathrate_B.c_x_data[breathrate_B.i] =
                breathrate_B.x4[breathrate_B.i1];
            breathrate_B.c_x_data[breathrate_B.i + 1] =
                breathrate_B.x4[breathrate_B.i2];
            breathrate_B.c_x_data[breathrate_B.i + 2] = breathrate_B.x4[i3];
            breathrate_B.c_x_data[breathrate_B.i + 3] = breathrate_B.x4[i4];
            breathrate_B.nLeft++;
        }

        breathrate_B.nQuartets <<= 2;
        breathrate_B.nLeft = (*x_size - breathrate_B.nQuartets) - 1;
        if (breathrate_B.nLeft + 1 > 0)
        {
            breathrate_B.i = 0;
            while (breathrate_B.i <= breathrate_B.nLeft)
            {
                breathrate_B.i1 = breathrate_B.nQuartets + breathrate_B.i;
                breathrate_B.idx4[breathrate_B.i] = (int16_T)(breathrate_B.i1 +
                    1);
                breathrate_B.x4[breathrate_B.i] =
                    breathrate_B.c_x_data[breathrate_B.i1];
                breathrate_B.i++;
            }

            breathrate_B.perm[1] = 0;
            breathrate_B.perm[2] = 0;
            breathrate_B.perm[3] = 0;
            switch (breathrate_B.nLeft + 1)
            {
              case 1:
                breathrate_B.perm[0] = 1;
                break;

              case 2:
                if (breathrate_B.x4[0] <= breathrate_B.x4[1])
                {
                    breathrate_B.perm[0] = 1;
                    breathrate_B.perm[1] = 2;
                }
                else
                {
                    breathrate_B.perm[0] = 2;
                    breathrate_B.perm[1] = 1;
                }
                break;

              default:
                if (breathrate_B.x4[0] <= breathrate_B.x4[1])
                {
                    if (breathrate_B.x4[1] <= breathrate_B.x4[2])
                    {
                        breathrate_B.perm[0] = 1;
                        breathrate_B.perm[1] = 2;
                        breathrate_B.perm[2] = 3;
                    }
                    else if (breathrate_B.x4[0] <= breathrate_B.x4[2])
                    {
                        breathrate_B.perm[0] = 1;
                        breathrate_B.perm[1] = 3;
                        breathrate_B.perm[2] = 2;
                    }
                    else
                    {
                        breathrate_B.perm[0] = 3;
                        breathrate_B.perm[1] = 1;
                        breathrate_B.perm[2] = 2;
                    }
                }
                else if (breathrate_B.x4[0] <= breathrate_B.x4[2])
                {
                    breathrate_B.perm[0] = 2;
                    breathrate_B.perm[1] = 1;
                    breathrate_B.perm[2] = 3;
                }
                else if (breathrate_B.x4[1] <= breathrate_B.x4[2])
                {
                    breathrate_B.perm[0] = 2;
                    breathrate_B.perm[1] = 3;
                    breathrate_B.perm[2] = 1;
                }
                else
                {
                    breathrate_B.perm[0] = 3;
                    breathrate_B.perm[1] = 2;
                    breathrate_B.perm[2] = 1;
                }
                break;
            }

            breathrate_B.i = 0;
            while (breathrate_B.i <= breathrate_B.nLeft)
            {
                breathrate_B.i1 = (int32_T)breathrate_B.perm[breathrate_B.i] - 1;
                breathrate_B.i2 = breathrate_B.nQuartets + breathrate_B.i;
                idx_data[breathrate_B.i2] = (int32_T)
                    breathrate_B.idx4[breathrate_B.i1];
                breathrate_B.c_x_data[breathrate_B.i2] =
                    breathrate_B.x4[breathrate_B.i1];
                breathrate_B.i++;
            }
        }

        if (*x_size > 1)
        {
            breathrate_B.nQuartets = (int32_T)b_x_idx_0;
            if (0 <= breathrate_B.nQuartets - 1)
            {
                memset(&breathrate_B.iwork_data[0], 0, (uint32_T)
                       breathrate_B.nQuartets * sizeof(int32_T));
            }

            b_x_idx_0 = (uint8_T)*x_size;
            breathrate_B.nQuartets = (int32_T)b_x_idx_0;
            if (0 <= breathrate_B.nQuartets - 1)
            {
                memset(&breathrate_B.xwork_data[0], 0, (uint32_T)
                       breathrate_B.nQuartets * sizeof(int32_T));
            }

            breathrate_merge_block(idx_data, breathrate_B.c_x_data, *x_size,
                                   breathrate_B.iwork_data,
                                   breathrate_B.xwork_data);
        }

        *x_size = c_x_size;
        breathrate_B.nQuartets = c_x_size;
        if (0 <= breathrate_B.nQuartets - 1)
        {
            memcpy(&x_data[0], &breathrate_B.c_x_data[0], (uint32_T)
                   breathrate_B.nQuartets * sizeof(int32_T));
        }
    }
}

static void breathrate_sort(int32_T x_data[], int32_T *x_size)
{
    breathrate_B.dim = 2;
    if (*x_size != 1)
    {
        breathrate_B.dim = 1;
    }

    if (breathrate_B.dim <= 1)
    {
        breathrate_B.b = *x_size - 1;
    }
    else
    {
        breathrate_B.b = 0;
    }

    breathrate_B.vwork_size = (int32_T)(uint8_T)(int32_T)(breathrate_B.b + 1);
    breathrate_B.vstride = 1;
    breathrate_B.c_k = 0;
    while (breathrate_B.c_k <= breathrate_B.dim - 2)
    {
        breathrate_B.vstride *= *x_size;
        breathrate_B.c_k = 1;
    }

    breathrate_B.dim = 0;
    while (breathrate_B.dim <= breathrate_B.vstride - 1)
    {
        breathrate_B.c_k = 0;
        while (breathrate_B.c_k <= breathrate_B.b)
        {
            breathrate_B.vwork_data_c[breathrate_B.c_k] =
                x_data[breathrate_B.c_k * breathrate_B.vstride +
                breathrate_B.dim];
            breathrate_B.c_k++;
        }

        breathrate_sortIdx_c(breathrate_B.vwork_data_c, &breathrate_B.vwork_size,
                             breathrate_B.vwork_data.data,
                             &breathrate_B.vwork_data.size);
        breathrate_B.c_k = 0;
        while (breathrate_B.c_k <= breathrate_B.b)
        {
            x_data[breathrate_B.dim + breathrate_B.c_k * breathrate_B.vstride] =
                breathrate_B.vwork_data_c[breathrate_B.c_k];
            breathrate_B.c_k++;
        }

        breathrate_B.dim++;
    }
}

static void findPeaksSeparatedByMoreThanMin(const real32_T y[149], const int32_T
    iPk_data[], const int32_T *iPk_size, int32_T idx_data[], int32_T *idx_size)
{
    uint8_T b_locs_temp_idx_0;
    if (*iPk_size == 0)
    {
        *idx_size = 0;
    }
    else
    {
        breathrate_B.y_size = *iPk_size;
        breathrate_B.loop_ub_b = *iPk_size;
        for (breathrate_B.trueCount = 0; breathrate_B.trueCount <
                breathrate_B.loop_ub_b; breathrate_B.trueCount++)
        {
            breathrate_B.y_data[breathrate_B.trueCount] =
                y[iPk_data[breathrate_B.trueCount] - 1];
        }

        breathrate_sortIdx(breathrate_B.y_data, &breathrate_B.y_size,
                           breathrate_B.sortIdx_data, &breathrate_B.sortIdx_size);
        breathrate_B.loop_ub_b = breathrate_B.sortIdx_size;
        for (breathrate_B.trueCount = 0; breathrate_B.trueCount <
                breathrate_B.loop_ub_b; breathrate_B.trueCount++)
        {
            breathrate_B.locs_temp_data[breathrate_B.trueCount] = (uint8_T)
                ((uint32_T)(uint8_T)(int32_T)
                 (iPk_data[breathrate_B.sortIdx_data[breathrate_B.trueCount] - 1]
                  - 1) + 1U);
        }

        b_locs_temp_idx_0 = (uint8_T)breathrate_B.sortIdx_size;
        breathrate_B.idelete_size_idx_0 = (int32_T)b_locs_temp_idx_0;
        breathrate_B.loop_ub_b = (int32_T)b_locs_temp_idx_0;
        if (0 <= breathrate_B.loop_ub_b - 1)
        {
            memset(&breathrate_B.idelete_data[0], 0, (uint32_T)
                   breathrate_B.loop_ub_b * sizeof(boolean_T));
        }

        breathrate_B.loop_ub_b = 0;
        while (breathrate_B.loop_ub_b <= breathrate_B.sortIdx_size - 1)
        {
            if (!breathrate_B.idelete_data[breathrate_B.loop_ub_b])
            {
                breathrate_B.trueCount = (int32_T)(uint8_T)((uint32_T)(uint8_T)
                    (int32_T)
                    (iPk_data[breathrate_B.sortIdx_data[breathrate_B.loop_ub_b]
                     - 1] - 1) + 1U);
                for (breathrate_B.b_i = 0; breathrate_B.b_i <
                        breathrate_B.idelete_size_idx_0; breathrate_B.b_i++)
                {
                    breathrate_B.idelete_data[breathrate_B.b_i] =
                        (breathrate_B.idelete_data[breathrate_B.b_i] ||
                         (((int32_T)breathrate_B.locs_temp_data[breathrate_B.b_i]
                           >= breathrate_B.trueCount - 20) && ((int32_T)
                           breathrate_B.locs_temp_data[breathrate_B.b_i] <=
                           breathrate_B.trueCount + 20)));
                }

                breathrate_B.idelete_data[breathrate_B.loop_ub_b] = false;
            }

            breathrate_B.loop_ub_b++;
        }

        breathrate_B.loop_ub_b = (int32_T)b_locs_temp_idx_0 - 1;
        breathrate_B.trueCount = 0;
        breathrate_B.b_i = 0;
        while (breathrate_B.b_i <= breathrate_B.loop_ub_b)
        {
            if (!breathrate_B.idelete_data[breathrate_B.b_i])
            {
                breathrate_B.trueCount++;
            }

            breathrate_B.b_i++;
        }

        breathrate_B.idelete_size_idx_0 = breathrate_B.trueCount;
        breathrate_B.trueCount = 0;
        breathrate_B.b_i = 0;
        while (breathrate_B.b_i <= breathrate_B.loop_ub_b)
        {
            if (!breathrate_B.idelete_data[breathrate_B.b_i])
            {
                breathrate_B.b_data[breathrate_B.trueCount] = (uint8_T)(int32_T)
                    (breathrate_B.b_i + 1);
                breathrate_B.trueCount++;
            }

            breathrate_B.b_i++;
        }

        *idx_size = breathrate_B.idelete_size_idx_0;
        for (breathrate_B.trueCount = 0; breathrate_B.trueCount <
                breathrate_B.idelete_size_idx_0; breathrate_B.trueCount++)
        {
            idx_data[breathrate_B.trueCount] = breathrate_B.sortIdx_data
                [(int32_T)breathrate_B.b_data[breathrate_B.trueCount] - 1];
        }

        breathrate_sort(idx_data, idx_size);
    }
}

static void breathrate_findpeaks(const real32_T Yin[149], real32_T Ypk_data[],
    int32_T *Ypk_size, real_T Xpk_data[], int32_T *Xpk_size)
{
    breathrate_getAllPeaksCodegen(Yin, breathrate_B.iFinite_data,
        &breathrate_B.iFinite_size, &breathrate_B.iPk_size,
        breathrate_B.idx_data, &breathrate_B.idx_size);
    breathrate_removeSmallPeaks(Yin, breathrate_B.iFinite_data,
        &breathrate_B.iFinite_size, breathrate_B.tmp_data,
        &breathrate_B.iPk_size);
    breathrate_do_vectors(breathrate_B.tmp_data, &breathrate_B.iPk_size,
                          breathrate_B.iFinite_data, &breathrate_B.iFinite_size,
                          breathrate_B.idx_data, &breathrate_B.idx_size,
                          &breathrate_B.ib_size);
    findPeaksSeparatedByMoreThanMin(Yin, breathrate_B.iFinite_data,
        &breathrate_B.iFinite_size, breathrate_B.idx_data,
        &breathrate_B.idx_size);
    breathrate_B.loop_ub = breathrate_B.idx_size;
    for (breathrate_B.i0 = 0; breathrate_B.i0 < breathrate_B.loop_ub;
            breathrate_B.i0++)
    {
        breathrate_B.iPk_data[breathrate_B.i0] =
            breathrate_B.iFinite_data[breathrate_B.idx_data[breathrate_B.i0] - 1];
    }

    *Ypk_size = breathrate_B.idx_size;
    breathrate_B.loop_ub = breathrate_B.idx_size;
    for (breathrate_B.i0 = 0; breathrate_B.i0 < breathrate_B.loop_ub;
            breathrate_B.i0++)
    {
        Ypk_data[breathrate_B.i0] = Yin[breathrate_B.iPk_data[breathrate_B.i0] -
            1];
    }

    *Xpk_size = breathrate_B.idx_size;
    breathrate_B.loop_ub = breathrate_B.idx_size;
    for (breathrate_B.i0 = 0; breathrate_B.i0 < breathrate_B.loop_ub;
            breathrate_B.i0++)
    {
        Xpk_data[breathrate_B.i0] = (real_T)(uint8_T)((uint32_T)(uint8_T)
            (int32_T)(breathrate_B.iPk_data[breathrate_B.i0] - 1) + 1U);
    }
}

static void breathrate_diff(const real_T x_data[], const int32_T *x_size, real_T
    y_data[], int32_T *y_size)
{
    int32_T ixLead;
    int32_T iyLead;
    real_T tmp2;
    int32_T m;
    real_T work_data_idx_0;
    uint8_T ySize_idx_0;
    if (*x_size == 0)
    {
        *y_size = 0;
    }
    else
    {
        ixLead = *x_size - 1;
        if (ixLead >= 1)
        {
            ixLead = 1;
        }

        if (ixLead < 1)
        {
            *y_size = 0;
        }
        else
        {
            ySize_idx_0 = (uint8_T)(int32_T)(*x_size - 1);
            *y_size = (int32_T)ySize_idx_0;
            if ((int32_T)ySize_idx_0 != 0)
            {
                ixLead = 1;
                iyLead = 0;
                work_data_idx_0 = x_data[0];
                for (m = 2; m <= *x_size; m++)
                {
                    tmp2 = work_data_idx_0;
                    work_data_idx_0 = x_data[ixLead];
                    y_data[iyLead] = x_data[ixLead] - tmp2;
                    ixLead++;
                    iyLead++;
                }
            }
        }
    }
}

static void breathrate_SystemCore_release(dspcodegen_BiquadFilter_breat_T *obj)
{
    if (obj->isInitialized == 1)
    {
        obj->isInitialized = 2;
    }
}

static void brea_LPHPFilterBase_releaseImpl(dsp_LowpassFilter_breathrate_T *obj)
{
    breathrate_SystemCore_release(obj->FilterObj);
    obj->NumChannels = -1;
}

static void breat_SystemCore_releaseWrapper(dsp_LowpassFilter_breathrate_T *obj)
{
    if (obj->isSetupComplete)
    {
        brea_LPHPFilterBase_releaseImpl(obj);
    }
}

static void breathrate_SystemCore_release_l(dsp_LowpassFilter_breathrate_T *obj)
{
    if (obj->isInitialized == 1)
    {
        breat_SystemCore_releaseWrapper(obj);
    }
}

static void breathrate_SystemCore_delete_l(dsp_LowpassFilter_breathrate_T *obj)
{
    breathrate_SystemCore_release_l(obj);
}

static void matlabCodegenHandle_matlabCod_l(dsp_LowpassFilter_breathrate_T *obj)
{
    if (!obj->matlabCodegenIsDeleted)
    {
        obj->matlabCodegenIsDeleted = true;
        breathrate_SystemCore_delete_l(obj);
    }
}

static void breathrate_SystemCore_delete(dspcodegen_BiquadFilter_breat_T *obj)
{
    breathrate_SystemCore_release(obj);
}

static void matlabCodegenHandle_matlabCodeg(dspcodegen_BiquadFilter_breat_T *obj)
{
    if (!obj->matlabCodegenIsDeleted)
    {
        obj->matlabCodegenIsDeleted = true;
        breathrate_SystemCore_delete(obj);
    }
}

void breathrate_step(void)
{
    dsp_BiquadFilter_0_breathrate_T *obj;
    if (breathrate_DW.obj.FilterObj->isInitialized != 1)
    {
        breathrate_DW.obj.FilterObj->isSetupComplete = false;
        breathrate_DW.obj.FilterObj->isInitialized = 1;
        breathrate_DW.obj.FilterObj->isSetupComplete = true;
        breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[0] =
            breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[0] =
            breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[1] =
            breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[1] =
            breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[2] =
            breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[2] =
            breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[3] =
            breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
        breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[3] =
            breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
    }

    obj = &breathrate_DW.obj.FilterObj->cSFunObject;
    breathrate_B.ixLead = 0;
    for (breathrate_B.iyLead = 0; breathrate_B.iyLead < 150; breathrate_B.iyLead
            ++)
    {
        breathrate_B.tmp2 = obj->P1_RTP1COEFF[0] *
            breathrate_U.In1[breathrate_B.ixLead];
        breathrate_B.tmp2 += obj->P1_RTP1COEFF[1] * obj->W0_ZERO_STATES[0];
        breathrate_B.tmp2 += obj->P1_RTP1COEFF[2] * obj->W0_ZERO_STATES[1];
        breathrate_B.tmp2 -= obj->P2_RTP2COEFF[0] * obj->W1_POLE_STATES[0];
        breathrate_B.tmp2 -= obj->P2_RTP2COEFF[1] * obj->W1_POLE_STATES[1];
        obj->W0_ZERO_STATES[1] = obj->W0_ZERO_STATES[0];
        obj->W0_ZERO_STATES[0] = breathrate_U.In1[breathrate_B.ixLead];
        obj->W1_POLE_STATES[1] = obj->W1_POLE_STATES[0];
        obj->W1_POLE_STATES[0] = breathrate_B.tmp2;
        breathrate_B.work = breathrate_B.tmp2;
        breathrate_B.tmp2 *= obj->P1_RTP1COEFF[3];
        breathrate_B.tmp2 += obj->P1_RTP1COEFF[4] * obj->W0_ZERO_STATES[2];
        breathrate_B.tmp2 += obj->P1_RTP1COEFF[5] * obj->W0_ZERO_STATES[3];
        breathrate_B.tmp2 -= obj->P2_RTP2COEFF[2] * obj->W1_POLE_STATES[2];
        breathrate_B.tmp2 -= obj->P2_RTP2COEFF[3] * obj->W1_POLE_STATES[3];
        obj->W0_ZERO_STATES[3] = obj->W0_ZERO_STATES[2];
        obj->W0_ZERO_STATES[2] = breathrate_B.work;
        obj->W1_POLE_STATES[3] = obj->W1_POLE_STATES[2];
        obj->W1_POLE_STATES[2] = breathrate_B.tmp2;
        breathrate_B.rtb_Lowpass2_m[breathrate_B.ixLead] = breathrate_B.tmp2;
        breathrate_B.ixLead++;
    }

    breathrate_B.ixLead = 1;
    breathrate_B.iyLead = 0;
    breathrate_B.work = breathrate_B.rtb_Lowpass2_m[0];
    for (breathrate_B.m = 0; breathrate_B.m < 149; breathrate_B.m++)
    {
        breathrate_B.tmp2 = breathrate_B.work;
        breathrate_B.work = breathrate_B.rtb_Lowpass2_m[breathrate_B.ixLead];
        breathrate_B.b_y1[breathrate_B.iyLead] =
            breathrate_B.rtb_Lowpass2_m[breathrate_B.ixLead] - breathrate_B.tmp2;
        breathrate_B.ixLead++;
        breathrate_B.iyLead++;
    }

    breathrate_findpeaks(breathrate_B.b_y1, breathrate_B.maxv_data,
                         &breathrate_B.maxv_size, breathrate_B.maxl_data,
                         &breathrate_B.maxl_size);
    breathrate_diff(breathrate_B.maxl_data, &breathrate_B.maxl_size,
                    breathrate_B.x_data, &breathrate_B.maxv_size);
    if (breathrate_B.maxv_size == 0)
    {
        breathrate_B.y = 0.0;
    }
    else
    {
        breathrate_B.y = breathrate_B.x_data[0];
        breathrate_B.iyLead = 2;
        while (breathrate_B.iyLead <= breathrate_B.maxv_size)
        {
            breathrate_B.y += breathrate_B.x_data[breathrate_B.iyLead - 1];
            breathrate_B.iyLead++;
        }
    }

    breathrate_Y.brate_data = (real32_T)(real_T)(breathrate_B.y / (real_T)
        breathrate_B.maxv_size / 15.0);
    breathrate_Y.len_data = (real32_T)breathrate_B.maxl_size;
}

void breathrate_initialize(void)
{
    rtmSetErrorStatus(breathrate_M, (NULL));
    (void) memset((void *)&breathrate_DW, 0,
                  sizeof(DW_breathrate_T));
    (void)memset(&breathrate_U, 0, sizeof(ExtU_breathrate_T));
    (void) memset((void *)&breathrate_Y, 0,
                  sizeof(ExtY_breathrate_T));

    {
        dspcodegen_BiquadFilter_breat_T *iobj_0;
        int32_T i;
        static const real32_T tmp[6] =
        {
            0.0498722494F, -0.0921436F, 0.0498722494F, 0.0229621362F,
            -0.0291773323F, 0.0229621362F
        };

        breathrate_DW.gobj_1.matlabCodegenIsDeleted = true;
        breathrate_DW.gobj_0.matlabCodegenIsDeleted = true;
        breathrate_DW.obj.matlabCodegenIsDeleted = true;
        breathrate_DW.obj.isInitialized = 0;
        breathrate_DW.obj.NumChannels = -1;
        breathrate_DW.obj.matlabCodegenIsDeleted = false;
        breathrate_DW.objisempty = true;
        iobj_0 = &breathrate_DW.gobj_0;
        breathrate_DW.obj.isSetupComplete = false;
        breathrate_DW.obj.isInitialized = 1;
        breathrate_DW.gobj_0.isInitialized = 0;
        iobj_0->cSFunObject.P0_ICRTP = 0.0F;
        for (i = 0; i < 6; i++)
        {
            iobj_0->cSFunObject.P1_RTP1COEFF[i] = tmp[i];
        }

        iobj_0->cSFunObject.P2_RTP2COEFF[0] = -1.92947519F;
        iobj_0->cSFunObject.P2_RTP2COEFF[1] = 0.945837557F;
        iobj_0->cSFunObject.P2_RTP2COEFF[2] = -1.85541236F;
        iobj_0->cSFunObject.P2_RTP2COEFF[3] = 0.863282F;
        iobj_0->cSFunObject.P3_RTP3COEFF[0] = 0.0F;
        iobj_0->cSFunObject.P3_RTP3COEFF[1] = 0.0F;
        iobj_0->cSFunObject.P3_RTP3COEFF[2] = 0.0F;
        iobj_0->cSFunObject.P4_RTP_COEFF3_BOOL[0] = false;
        iobj_0->cSFunObject.P4_RTP_COEFF3_BOOL[1] = false;
        iobj_0->cSFunObject.P4_RTP_COEFF3_BOOL[2] = false;
        iobj_0->cSFunObject.P5_IC2RTP = 0.0F;
        breathrate_DW.gobj_0.matlabCodegenIsDeleted = false;
        breathrate_DW.obj.FilterObj = &breathrate_DW.gobj_0;
        breathrate_DW.obj.NumChannels = 1;
        breathrate_DW.obj.isSetupComplete = true;
        if (breathrate_DW.obj.FilterObj->isInitialized == 1)
        {
            breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[0] =
                breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[0] =
                breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[1] =
                breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[1] =
                breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[2] =
                breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[2] =
                breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W0_ZERO_STATES[3] =
                breathrate_DW.obj.FilterObj->cSFunObject.P0_ICRTP;
            breathrate_DW.obj.FilterObj->cSFunObject.W1_POLE_STATES[3] =
                breathrate_DW.obj.FilterObj->cSFunObject.P5_IC2RTP;
        }
    }
}

void breathrate_terminate(void)
{
    matlabCodegenHandle_matlabCod_l(&breathrate_DW.obj);
    matlabCodegenHandle_matlabCodeg(&breathrate_DW.gobj_0);
    matlabCodegenHandle_matlabCodeg(&breathrate_DW.gobj_1);
}
